package org.jeecg.flowable.mapper.task;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.ibatis.annotations.Mapper;
import org.jeecg.flowable.entity.task.BpmProcessInstanceExt;
import org.springframework.util.StringUtils;

@Mapper
public interface BpmProcessInstanceExtMapper extends BaseMapper<BpmProcessInstanceExt> {

    default PageResult<BpmProcessInstanceExt> selectPage(Long userId, BpmProcessInstanceMyPageReqVO reqVO) {

        LambdaQueryWrapper<BpmProcessInstanceExt> queryWrapper =  new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(userId)){
            queryWrapper.eq(BpmProcessInstanceExt::getStartUserId, userId);
        }
        if(StringUtils.hasText(reqVO.getName())){
            queryWrapper.like(BpmProcessInstanceExt::getName, reqVO.getName());
        }

        if(ObjectUtil.isNotEmpty(reqVO.getProcessDefinitionId())){
            queryWrapper.eq(BpmProcessInstanceExt::getProcessDefinitionId, reqVO.getProcessDefinitionId());
        }

        if(ObjectUtil.isNotEmpty(reqVO.getCategory())){
            queryWrapper.eq(BpmProcessInstanceExt::getCategory, reqVO.getCategory());
        }
        if(ObjectUtil.isNotEmpty(reqVO.getStatus())){
            queryWrapper.eq(BpmProcessInstanceExt::getStatus, reqVO.getStatus());
        }
        if(ObjectUtil.isNotEmpty(reqVO.getResult())){
            queryWrapper.eq(BpmProcessInstanceExt::getResult, reqVO.getResult());
        }

        Object val1 = get(reqVO.getCreateTime(), 0);
        Object val2 = get(reqVO.getCreateTime(), 1);
        if (val1 != null && val2 != null) {
            queryWrapper.between(BpmProcessInstanceExt::getCreateTime, val1, val2);
        }
        if (val1 != null) {
             queryWrapper. ge(BpmProcessInstanceExt::getCreateTime, val1);
        }
        if (val2 != null) {
             queryWrapper.le(BpmProcessInstanceExt::getCreateTime, val2);
        }
        queryWrapper.orderByDesc(BpmProcessInstanceExt::getId);

        return selectPage(reqVO, queryWrapper);
    }

    public static <T> T get(T[] array, int index) {
        if (null == array || index >= array.length) {
            return null;
        }
        return array[index];
    }


    default BpmProcessInstanceExt selectByProcessInstanceId(String processInstanceId) {
        LambdaQueryWrapper<BpmProcessInstanceExt> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(BpmProcessInstanceExt::getProcessInstanceId, processInstanceId);
        return selectOne(queryWrapper);
    }

    default void updateByProcessInstanceId(BpmProcessInstanceExt updateObj) {
        update(updateObj, new LambdaQueryWrapper<BpmProcessInstanceExt>()
                .eq(BpmProcessInstanceExt::getProcessInstanceId, updateObj.getProcessInstanceId()));


    }

}
